<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.dict.entity.dictName')">
          <a-input placeholder="" v-decorator.trim="[ 'dictName', validatorRules.dictName]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.dict.entity.dictCode')">
          <a-input placeholder="" v-decorator.trim="[ 'dictCode', validatorRules.dictCode]"/>
        </a-form-item>

        <a-form-item
          v-if="categoryType=='1'"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.dict.entity.category')">
          <a-select
            v-decorator="[ 'category']"
            placeholder="">
            <!--<a-select-option value="0">平台</a-select-option>-->
            <a-select-option value="1">{{$t('m.system.dict.other.option_category1')}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.dict.entity.description')">
          <a-input v-decorator="[ 'description']"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { addDict, editDict, duplicateCheck } from '@/api/api'

  export default {
    name: 'DictModal',
    props:{
      categoryType: {
        type: String,
        default: '1'
      },
      groupId: {
        type: String,
        default: null
      }
    },
    data() {
      return {
        value: 1,
        title: this.$t('m.common.label.operate'),
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          dictName: { rules: [{ required: true, message: this.$t('m.system.dict.verify.dictName') }] },
          dictCode: {
            rules: [{ required: true, message: this.$t('m.system.dict.verify.dictCode') },
              { validator: this.validateDictCode }]
          },
        }
      }
    },
    created() {
    },
    methods: {
      validateDictCode(rule, value, callback) {
        let reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
        if(reg.test(value)){
          callback(this.$t('m.common.tip.code_format'))
        }
        // 重复校验
        var params = {
          tableName: 'sys_dict',
          fieldName: 'dict_code',
          fieldVal: value,
          dataId: this.model.id
        }
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback(res.message)
          }
        })
      },
      handleChange(value) {
        this.model.status = value
      },
      add() {
        let record = {};
        if(this.categoryType == '2'){
          record.category = '2';
        }else{
          record.category = '1';
        }
        record.groupId = this.groupId;
        this.edit(record)
      },
      edit(record) {
        if (record.id) {
          this.visiblekey = true
        } else {
          this.visiblekey = false
        }
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model, 'dictName', 'dictCode', 'description','category'))
        })
      },
      // 确定
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            values.dictName = (values.dictName || '').trim()
            values.dictCode = (values.dictCode || '').trim()
            values.description = (values.description || '').trim()
            let formData = Object.assign(this.model, values)
            let obj
            //console.log(formData)
            if (!this.model.id) {
              obj = addDict(formData)
            } else {
              obj = editDict(formData)
            }
            obj.then((res) => {
              if (res.success) {
                that.messageUtil.success(res.message)
                that.$emit('ok')
              } else {
                that.messageUtil.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      // 关闭
      handleCancel() {
        this.close()
      },
      close() {
        this.$emit('close')
        this.visible = false
      }
    }
  }
</script>
